#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx
import  wx.grid as Grid
import pymssql

# 从数据库里获得数据生成特定格式的数据源-------------------
class DataSource:
    def __init__(self,aSet):
        self.RKEY = aSet[0]
        self.CUST_CODE = aSet[1]
        self.CUST_NAME = aSet[2]
        self.SALES_CODE = aSet[3]
        self.QUOTE_DATE = aSet[4]
        self.QUOTE_LIFE = aSet[5]
        self.ORDER_AMOUNT = aSet[6]
        self.ORDER_REC_TIME = aSet[7]
        self.ORDER_OUT_TIME = aSet[8]
        self.CON_NO = aSet[9]
        self.ABN_AMOUNT = aSet[10]
        self.ABN_CAUSES = aSet[11]
        self.CON_FAX_TIME = aSet[12]
        self.CON_EDIT_TIME = aSet[13]
        self.CON_BACK_TIME = aSet[14]
        self.CON_EDIT_CAUSES = aSet[15]
        self.HALF_STATUS = aSet[16]
        self.TWO_STATUS = aSet[17]
        self.DAY_STATUS = aSet[18]
        self.PUT_USER = aSet[19]
        self.PUT_TIME = aSet[20]
        self.NO_BACK_CAUSES = aSet[21]
        self.CON_MAKE_CYCLE = aSet[22]
        self.CON_PUT_CYCLE = aSet[23]
        self.REMARK = aSet[24]
#----------------------------------------------------------------------------

#定义基本表，覆盖其中的方法--------------------------------------------------      
class MainTable(Grid.PyGridTableBase):
    
    colLabels=(u"客户代码",u"客户名称",u"业务员代码",u"报价日期",u"报价有效期",
               u"订单数量",u"订单接收时间",u"订单分发时间",u"合同号",u"异常数量",
               u"异常原因",u"传真时间",u"修改时间",u"回传时间",u"合同修改原因",
               u"半小时合同回传情况",u"2小时合同跟进情况",u"1工作日合同跟进情况",
               u"投产人员",u"投产单发放时间",u"未回传合同原因",u"合同制作周期",
               u"合同投产周期",u"备注")
    
    colAttrs=("CUST_CODE","CUST_NAME","SALES_CODE","QUOTE_DATE","QUOTE_LIFE",
              "ORDER_AMOUNT","ORDER_REC_TIME","ORDER_OUT_TIME","CON_NO","ABN_AMOUNT",
              "ABN_CAUSES","CON_FAX_TIME","CON_EDIT_TIME","CON_BACK_TIME","CON_EDIT_CAUSES",
              "HALF_STATUS","TWO_STATUS","DAY_STATUS","PUT_USER","PUT_TIME","NO_BACK_CAUSES",
              "CON_MAKE_CYCLE","CON_PUT_CYCLE","REMARK")
    
    def __init__(self,datas):
        Grid.PyGridTableBase.__init__(self)
        self.datas=datas
        self._rows = self.GetNumberRows()
        self._cols = self.GetNumberCols()
        
    def GetNumberRows(self):
        return len(self.datas)
        
    def GetNumberCols(self):
        return 24
    
    def GetColLabelValue(self,col):
        return self.colLabels[col]
    
    def GetRowLabelValue(self, row):
        return self.datas[row].RKEY
        
    #def GetRowLabelValue(self, row):
        #return row
        
    def IsEmptyCell(self,row,col):
        return False
           
    def GetValue(self,row,col):
        #在行循环，用列属性取值，为Null的单元格用''填充
        value=getattr(self.datas[row],self.colAttrs[col])
        if value is not None:
            return value
        else:
            return ''
        
    def SetValue(self,row,col,value):
        pass
    
    def MakeCellVisible(self,row,col):
        return True
    
    def DeleteRows(self, rows):
        deleteCount = 0
        rows = rows[:]
        rows.sort()
        for i in rows:
            self.datas.pop(i-deleteCount)
            deleteCount += 1
            
    #定义刷新视图---------------------------------------------------------       
    def ResetView(self, grid):
        #在删除一个row以后刷新显示
        grid.BeginBatch()

        for current, new, delmsg, addmsg in [(self._rows, self.GetNumberRows(), Grid.GRIDTABLE_NOTIFY_ROWS_DELETED, Grid.GRIDTABLE_NOTIFY_ROWS_APPENDED)]:
            if new < current:
                msg = Grid.GridTableMessage(self,delmsg,new,current-new)
                grid.ProcessTableMessage(msg)
            elif new > current:
                msg = Grid.GridTableMessage(self,addmsg,new-current)
                grid.ProcessTableMessage(msg)
                self.UpdateValues(grid)

        grid.EndBatch()
        
        self._rows = self.GetNumberRows()
        self._cols = self.GetNumberCols()

        # 更新滚动和显示的网格部分
        grid.AdjustScrollbars()
        grid.ForceRefresh()


    def UpdateValues(self, grid):
        #更新所有显示区域的值
        # 发送事件到网格中以更新的所有值
        msg = Grid.GridTableMessage(self, Grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
        grid.ProcessTableMessage(msg)
        
    #------------------------------------------------------------------------------
        
#--------------------------------------------------------------------   

#以下为测试单元--------------------------------------------------------
class TestGrid(Grid.Grid):
    def __init__(self, parent):
        Grid.Grid.__init__(self, parent, -1)
        DataSet=[]
        conn=pymssql.connect(host='192.168.1.41',user='sa',password='',database='testdb2',charset='GBK')
        cur=conn.cursor()
        cur.execute("select * from CON_ABN")
        for item in cur:
            DataSet.append(DataSource(item))    
        cur.close()
        conn.close()
        DataTableBase = MainTable(DataSet)
        self.SetTable(DataTableBase)                   

class TestFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, "A Grid",size=(275, 275))
        grid = TestGrid(self)
 
if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = TestFrame(None)
    frame.Show(True)
    app.MainLoop()
